package com.geostar.constructionland.analyse.service.handler.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.geostar.constructionland.analyse.entity.HttpResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResultStbhhx;
import com.geostar.constructionland.analyse.entity.model.AnalyseModel;
import com.geostar.constructionland.analyse.entity.resulthandler.HandlerResult;
import com.geostar.constructionland.analyse.service.analyseresult.IStbhhxService;
import com.geostar.constructionland.analyse.service.handler.IResultHandler;
import com.geostar.constructionland.analyse.service.handler.impl.util.XmlUtil;
import com.geostar.constructionland.common.dao.YzGeoJsonDao;
import com.geostar.constructionland.common.entity.logs.mongolog.YzGeoJson;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.UUID;

/**
 * @Author makoto_zhu
 * @Date 2020-12-28 05:19:47
 * @Description
 **/
@Component
@Slf4j
public class StbhhxHandle implements IResultHandler {

    @Autowired
    ObjectMapper objectMapper;

    @Autowired
    IStbhhxService stbhhxService;

    @Autowired
    private YzGeoJsonDao yzGeoJsonDao;

    @Override
    public HandlerResult handler(AnalyseModel analyseModel, HttpResult httpResult) throws Exception {
        log.info("处理生态保护红线分析服务,状态码:{}", httpResult.getCode());
        HandlerResult result = HandlerResult.createBySuccess();
        if (HttpStatus.SC_OK == httpResult.getCode()) {
            /**解析xml格式,获取JSON数据**/
            String newBody = XmlUtil.parseXml(httpResult.getBody());
            httpResult.setBody(newBody);
            /**保存占压信息**/
            AlyResult alyResult = objectMapper.readValue(httpResult.getBody(), AlyResult.class);
            log.info("body:" + httpResult.getBody());
            if (HttpStatus.SC_OK == alyResult.getStatus()) {
                List<AlyResultStbhhx> stbhhxes = objectMapper.readValue(alyResult.getAnalysisResult().getResultRecords()
                        , new TypeReference<List<AlyResultStbhhx>>() {
                        });
                /****如果不为空，存入数据库****/
                if (CollectionUtils.isNotEmpty(stbhhxes)) {
                    for (AlyResultStbhhx alyResultStbhhx : stbhhxes) {
                        // 单独处理Json数据至Mongo
                        YzGeoJson yzGeoJson = new YzGeoJson();
                        String yzGeoJsonId = UUID.randomUUID().toString().replaceAll("-", "");
                        yzGeoJson.setId(yzGeoJsonId);
                        yzGeoJson.setGeoJson(alyResultStbhhx.getYzgeojson());
                        yzGeoJsonDao.saveYzGeoJson(yzGeoJson);
                        // 增加冗余数据
                        alyResultStbhhx.setMongoId(yzGeoJsonId);
                        alyResultStbhhx.setFxbs(analyseModel.getAlyId());
                    }
                    stbhhxService.saveBatch(stbhhxes);
                }
            } else {
                throw new Exception(alyResult.getMessage());
            }
        }
        return result;

    }
}
